home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / instools / prelude.zip / LINGUA13.EXE / UI_TEXT.C < prev    next >
C/C++ Source or Header  |  1994-02-17  |  6KB  |  177 lines

  1. /* --------------------------------------------*\
  2. | ui_text.c (version 1.3) - (C) SichemSoft 1993 |
  3. | Roghorst 160, 6708 KS Wageningen, Netherlands |
  4. | module  for language independent applications |
  5. | author: Anneke Sicherer-Roetman, date: 930916 |
  6. \* --------------------------------------------*/
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <ctype.h>
  11. #include "\bcpp\tools\lingua\lingua.h"
  12. #include "\bcpp\tools\lingua\files.inc"
  13.  
  14. char **ui_text;                       /* pointers into memory text buffer */
  15. unsigned _ui_memcount,_ui_filcount;   /* undocumented global variables */
  16. static char *ui_textbuffer;           /* memory text buffer */
  17. static unsigned long *ui_file;        /* pointers into file */
  18. static char *ui_filebuffer;           /* file text buffer */
  19. static char **ui_fileptr;             /* pointers into array from file */
  20. static unsigned itemchecksum=0;       /* for checksumming file items */
  21.  
  22. /* converts string to all lower case (common ANSI extension) */
  23. static char *strlwr(char *s)
  24. {
  25.    char *p;
  26.    for (p=s; *s; s++) if (isupper(*s)) *s=tolower(*s);
  27.    return p;
  28. }
  29.  
  30. /* decrypts text and computes checksum */
  31. static unsigned decrypt(char *buffer,unsigned long size)
  32. {
  33.    register unsigned long offset;
  34.    unsigned checksum;
  35.    char *q;
  36.  
  37.    for (offset=0,checksum=0,q=buffer; offset<size; offset++,q++) {
  38.       *q^=UIT_ENCRYPT; checksum+=(unsigned char)(*q);
  39.    }
  40.    return checksum;
  41. }
  42.  
  43. /* loads text from file into ui_filebuffer */
  44. static char *loadtext(unsigned long pos,unsigned size)
  45. {
  46.    static unsigned long oldpos=(unsigned long)(-1L);
  47.  
  48.    if (pos==oldpos) return ui_filebuffer;
  49.    if (!fileseek(pos)) return NULL;
  50.    if (ui_filebuffer) free(ui_filebuffer);
  51.    ui_filebuffer=(char *)malloc(size*sizeof(char));
  52.    if (!ui_filebuffer) return NULL;
  53.    if (!fileread(ui_filebuffer,size*sizeof(char))) return NULL;
  54.    itemchecksum=decrypt(ui_filebuffer,size);
  55.    oldpos=pos; /* next time only load if necessary */
  56.    return ui_filebuffer;
  57. }
  58.  
  59. /* loads text (no array) from file into ui_filebuffer */
  60. /* note: the programmer should not call this routine directly */
  61. char *ui_filetext(unsigned pos)
  62. {
  63.    return loadtext(ui_file[pos],ui_file[pos+1]-ui_file[pos]);
  64. }
  65.  
  66. /* loads text array from file into ui_filebuffer */
  67. /* note: the programmer should not call this routine directly */
  68. char **ui_filearray(unsigned pos)
  69. {
  70.    unsigned size; register int i;
  71.  
  72.    if (!fileseek(ui_file[pos])) return NULL;
  73.    if (!fileread(&size,sizeof(size))) return NULL;
  74.    if (!loadtext(filetell(),ui_file[pos+size]-ui_file[pos]-sizeof(size)))
  75.       return NULL;
  76.    if (ui_fileptr) free(ui_fileptr);
  77.    ui_fileptr=(char **)malloc(size*sizeof(char*));
  78.    if (!ui_fileptr) return NULL;
  79.    ui_fileptr[0]=ui_filebuffer;
  80.    for (i=1; i<size; i++)
  81.       ui_fileptr[i]=ui_filebuffer+ui_file[pos+i]-ui_file[pos]-sizeof(size);
  82.    return ui_fileptr;
  83. }
  84.  
  85. /* opens file, loads text and offsets into memory */
  86. /* call this routine at the very beginning of the application */
  87. int ui_loadtext(char *fname,char *vers)
  88. /* fname is name of desired .etf file without extension
  89.    vers is optional version number, may also be "" */
  90. {
  91.    char *q;
  92.    unsigned len,i;
  93.    unsigned checksum,checksum1=0,checksum2=0;
  94.    char buf[256],file[81];
  95.    unsigned long offset,size;
  96.  
  97.    if (ui_text || ui_file) return FALSE;
  98.  
  99.    /* open encrypted text file */
  100.    strcpy(file,fname);
  101.    if (!strchr(file,'.')) strcat(file,".etf");
  102.    if (!fileopen(file)) return FALSE;
  103.  
  104.    /* read file header */
  105.    if (!vers) vers="";
  106.    q=strrchr(file,'\\'); if (q) q++; else q=file;
  107.    len=strlen(q)+strlen(vers);
  108.    if (!fileread(buf,(len+1)*sizeof(char))) return FALSE;
  109.    strlwr(buf); strlwr(q); strlwr(vers);
  110.    if (strncmp(buf,q,strlen(q))) return FALSE;
  111.    if (strncmp(buf+strlen(q),vers,strlen(vers))) return FALSE;
  112.  
  113.    /* read number of items */
  114.    if (!fileread(&checksum,sizeof(checksum))) return FALSE;
  115.    if (!fileread(&_ui_memcount,sizeof(_ui_memcount))) return FALSE;
  116.    if (!fileread(&_ui_filcount,sizeof(_ui_filcount))) return FALSE;
  117.    if (!fileread(&size,sizeof(size))) return FALSE;
  118.    if (_ui_memcount) {
  119.       /* reserve space */
  120.       ui_text=(char **)malloc(_ui_memcount*sizeof(char *));
  121.       if (!ui_text) return FALSE;
  122.       ui_textbuffer=(char *)malloc(size*sizeof(char));
  123.       if (!ui_textbuffer) return FALSE;
  124.  
  125.       /* read offsets and compute pointers */
  126.       for (i=0; i<_ui_memcount; i++) {
  127.          if (!fileread(&offset,sizeof(offset))) return FALSE;
  128.          ui_text[i]=ui_textbuffer+offset;
  129.       }
  130.    }
  131.  
  132.    if (_ui_filcount) {
  133.       /* reserve space */
  134.       ui_file=(unsigned long *)malloc((_ui_filcount+1)*sizeof(unsigned long));
  135.       if (!ui_file) return FALSE;
  136.  
  137.       /* read offsets of file items */
  138.       for (i=0; i<_ui_filcount+1; i++) {
  139.          if (!fileread(&offset,sizeof(offset))) return FALSE;
  140.          ui_file[i]=offset;
  141.       }
  142.    }
  143.  
  144.    if (_ui_memcount) {
  145.       /* read text lines and decrypt */
  146.       if (!fileread(ui_textbuffer,size*sizeof(char))) return FALSE;
  147.       checksum1=decrypt(ui_textbuffer,size);
  148.    }
  149.  
  150.    if (_ui_filcount) {
  151.       /* read file items and decrypt for checksumming */
  152.       for (i=0; i<_ui_filcount; i++) {
  153.          q=ui_filetext(i); if (!q) return FALSE;
  154.          checksum2+=itemchecksum;
  155.       }
  156.    }
  157.  
  158.    /* close file if no file items present */
  159.    if (!_ui_filcount) fileclose();
  160.  
  161.    /* succesful load if checksum is ok */
  162.    return (checksum==checksum1+checksum2);
  163. } /* ui_loadtext */
  164.  
  165. /* frees all space and closes file if necessary */
  166. /* call this routine at the very end of the application */
  167. void ui_unloadtext(void)
  168. {
  169.    if (ui_text) { free(ui_text); ui_text=NULL; }
  170.    if (ui_textbuffer) { free(ui_textbuffer);  ui_textbuffer=NULL; }
  171.    if (ui_file) { free(ui_file); ui_file=NULL; }
  172.    if (ui_filebuffer) { free(ui_filebuffer); ui_filebuffer=NULL; }
  173.    if (ui_fileptr) { free(ui_fileptr); ui_fileptr=NULL; }
  174.    fileclose();
  175. } /* ui_unloadtext */
  176.  
  177.